﻿using System.Linq;
using System.Reflection;
using System.Web.Compilation;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.WebApi;
using ESoft.Core.DAO;
using ESoft.Core.Initialize;
using ESoft.Core.Logging;
using ESoft.Core.Logging.Log4Net;

namespace ESoft.API
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {

            DatabaseInitialize();
            LoggingInitialize();

            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            AutoFacBootStrapper.CoreAutoFacInit(); 
        }

        private static void DatabaseInitialize()
        {
            var assmblyArr = BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToArray();
            DatabaseInitializer.AddMapperAssembly(assmblyArr);
            DatabaseInitializer.Initialize();
        }

        private static void LoggingInitialize()
        {
            Log4NetLoggerAdapter adapter = new Log4NetLoggerAdapter();
            LogManager.AddLoggerAdapter(adapter);
        }
    }
}
